//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#include <buola/algorithm/split.h>
#include <clocale>
#include <buola/io.h>
#include <locale>

void print_vector(const std::vector<std::wstring> &pV)
{
    using namespace buola;
    for(int i=0;i<pV.size();i++)
        msg_info() << "[" << pV[i] << "]\n";
    msg_info() << "\n";
}

int main()
{
    using namespace buola;
    std::locale::global(std::locale(""));
    msg_info() << std::setlocale(LC_ALL,nullptr) << "\n";
    msg_info() << std::locale().name() << "\n";
    std::wstring lString(L"ñññññññññññññññññññññññññññññññññññññññ  ññññññññ ñññ ñññ ññññññ  ñññññññññññ ñññññ ");

    int lC=0;
    start_timer();
    for(int i=0;i<1000000;i++)
    {
        for(int j=0;j<lString.length();j++)
            if(iswupper(towupper(lString[j])))
                lC++;
            
    }
    end_timer();
    msg_info() << lC << "\n";
    
    lC=0;
    start_timer();
    std::locale lLocale;
    const auto &lFacet=std::use_facet<std::ctype<wchar_t>>(std::locale());
    for(int i=0;i<1000000;i++)
    {
        for(int j=0;j<lString.length();j++)
            if(lFacet.is(std::ctype_base::upper,lFacet.tolower(lString[j])))
                lC++;
            
    }
    end_timer();
    msg_info() << lC << "\n";
    msg_info() << "with buola (1000000 reps)\n";
    {
        int lC1=0,lC2=0;
        start_timer();
        for(int i=0;i<1000000;i++)
        {
            std::vector<CRange<std::wstring::const_iterator>> lV;
            buola::split(lString,lV,fn::is_ascii_space());
            lC1+=lV.size();
            lV.clear();
            split(lString,lV,fn::is_ascii_space(),nDeleteEmpty);
            lC2+=lV.size();
        }
        end_timer();
        msg_info() << lC1 << " " << lC2 << "\n";
        {
            std::vector<std::wstring> lV;
            split(lString,lV,fn::is_ascii_space());
            print_vector(lV);
            lV.clear();
            split(lString,lV,fn::is_ascii_space(),nDeleteEmpty);
            print_vector(lV);
        }
    }
}
